<?php
/**
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');


class promotionCon extends rootCon
{
    public function getAction() {
        $uid = mylogin::getUid(true);
        $obj = new PromotionConfig();
        $obj->promotionConfigId = r_int('promotionConfigId');
        $obj->find();


        $time = date("Y-m-d H:i:s");
        if($obj->expire <= $time) $this->j(-1,"优惠券不存在或已经过期");

        $promotion = new Promotion();
        $promotion->uid = $uid;
        $promotion->fromPromotionConfigId = $obj->promotionConfigId;
        $find = $promotion->find();
        if($find) $this->j(-2,"您已经领取过这个优惠券");

        $promotion->expire = $obj->expire;
        $promotion->created = date('Y-m-d H:i:s');
        $promotion->discountPrice = $obj->discountPrice;
        $promotion->limitPrice = $obj->limitPrice;
        $promotion->status = PROMOTION_UNUSED;
        $promotion->save();

        $sql = "SELECT COUNT(1) AS NUM FROM ".$promotion->table()." WHERE uid=$uid";
        $one = $promotion->one($sql);
        $user = new User();
        $user->find($uid);
        $user->promotionCount = $one['NUM'];
        $user->save();

        $this->j(0,'详情',$promotion->arr);
    }

    public function recommendAction() {
        $uid = mylogin::getUid(true);
        $obj = new Promotion();

        $price = floatval(r('price'));
        if($price<=0){
            $this->j(-1,"订单价格错误",['price'=>$price]);
        }

        $time = date('Y-m-d H:i:s');
        $sql = "SELECT * FROM ".$obj->table()." WHERE uid=".$uid." AND expire>'$time' AND status=".PROMOTION_UNUSED." AND limitPrice<= $price ORDER BY discountPrice DESC LIMIT 1";
        //echo $sql;
        $one = $obj->getOne($sql);

        $this->j(0,'',$one);
    }

    public function listAction() {
        $uid = mylogin::getUid(true);
        $obj = new PromotionConfig();
        $time = date("Y-m-d H:i:s");
        $con = "expire>'$time'";
        $all = $obj->all($con);
        $i = 0;
        foreach($all as $item){
            $fromPromotionConfigId = $item['promotionConfigId'];
            $config = new Promotion();
            $config->fromPromotionConfigId = $fromPromotionConfigId;
            $config->uid = $uid;
            $find = $config->find();
            if($find){
                $all[$i]['iHave'] = true;
            }else{
                $all[$i]['iHave'] = false;
            }
            $i++;
        }//end foreach

        $this->j(0, "列表",$all);
    }



    public function mineAction() {
        $uid = mylogin::getUid(true);
        $obj = new Promotion();
        $all = $obj->all(['uid',$uid]);

        $i = 0;
        foreach($all as $item){
            if($item['expire'] < date('Y-m-d H:i:s') && $item['status']==PROMOTION_UNUSED){//update
                $promotion = new Promotion();
                $promotion->find($item['promotionId']);
                $promotion->status = PROMOTION_EXPIRED;
                $promotion->save();
                $all[$i]['status'] = PROMOTION_EXPIRED;
            }
            $i++;
        }

        $this->j(0, "列表",$all);
    }
}



